<!--  -->
<template>
  <div class="main-content userManagement">
    <div class="header">
      <div class="add-company-box">
        <el-button type="primary" @click="openAddCompanyModal()"
          >新增公司</el-button
        >
      </div>
      <div class="screen-box">
        <el-form ref="screenForm" :model="screenForm">
          <el-form-item label="所属品牌">
            <el-select v-model="screenForm.brand" placeholder="请选择所属品牌">
              <el-option
                v-for="(item, index) in brandOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公司名称">
            <el-input
              v-model="screenForm.companyName"
              placeholder="请输入公司名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="当前状态">
            <el-select v-model="screenForm.state" placeholder="请选择当前状态">
              <el-option
                v-for="(item, index) in stateOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="注册时间">
            <el-date-picker
              v-model="screenForm.registered"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
          <el-button class="search-btn" type="primary">查询</el-button>
        </el-form>
      </div>
    </div>

    <div class="data-box">
      <div class="titleTextBox">
        <div class="titleText">
          <i class="iconfont iconliebiao"></i>
          <span class="text">数据信息</span>
        </div>
      </div>

      <div class="data-item-list">
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">全部</div>
        </el-card>
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">正常</div>
        </el-card>
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">7天内到期</div>
        </el-card>
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">30天内到期</div>
        </el-card>
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">已到期</div>
        </el-card>
        <el-card class="data-item">
          <div class="num">8</div>
          <div class="title">禁用</div>
        </el-card>
      </div>

      <div class="data-table-list">
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          :header-cell-style="{
            color: '#000',
            fontSize: '14px',
            backgroundColor: '#f8f8f8',
          }"
          :row-style="{
            borderRadius: '20px',
          }"
        >
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="companyName" label="公司名称">
          </el-table-column>
          <el-table-column prop="brand" label="所属品牌"> </el-table-column>
          <el-table-column prop="state" label="当前状态" width="150">
          </el-table-column>
          <el-table-column prop="registered" label="注册时间" width="200">
          </el-table-column>
          <el-table-column prop="dueDate" label="到期时间" width="200">
          </el-table-column>
          <el-table-column prop="dueDays" label="到期天数" width="150">
          </el-table-column>
          <el-table-column label="操作" width="100" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handleClick(scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[100, 200, 300, 400]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="400"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 新增公司 -->
    <bi-modal
      title="新增公司"
      modalType="form"
      sureText="保存"
      :showModal="showAddCompanyModal"
      @cancel="showAddCompanyModal = false"
      @submit="submitAddCompany()"
    >
      <template v-slot:body>
        <div class="add-company-box">
          <el-form ref="addCompanyForm" :model="addCompanyForm">
            <el-form-item label="所属品牌">
              <el-radio-group v-model="addCompanyForm.isHadBrand">
                <el-radio :label="true">已有</el-radio>
                <el-radio :label="false">新建</el-radio>
              </el-radio-group>
              <div class="select-input-box">
                <el-select
                  v-model="addCompanyForm.brand"
                  placeholder="请选择所属品牌"
                  v-show="addCompanyForm.isHadBrand"
                >
                  <el-option
                    v-for="(item, index) in brandOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
                <el-input
                  v-show="!addCompanyForm.isHadBrand"
                  v-model="addCompanyForm.brand"
                  placeholder="请输入品牌名称"
                ></el-input>
              </div>
            </el-form-item>

            <el-form-item label="公司名称">
              <el-input
                v-model="addCompanyForm.companyName"
                placeholder="请输入公司名称"
              ></el-input>
            </el-form-item>

            <el-form-item label="到期时间">
              <el-date-picker
                v-model="addCompanyForm.dueDate"
                type="datetime"
                placeholder="选择到期时间"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="启用状态">
              <el-switch v-model="addCompanyForm.state" active-text="启用">
              </el-switch>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </bi-modal>

    <!-- 编辑公司数据 -->
    <bi-modal
      :title="editCompanyForm.title"
      modalType="form"
      sureText="保存"
      :showModal="showEditCompanyModal"
      @cancel="showEditCompanyModal = false"
      @submit="submitEditCompany()"
    >
      <template v-slot:body>
        <div class="add-company-box">
          <el-form ref="addCompanyForm" :model="editCompanyForm">
            <el-form-item label="所属品牌">
              <el-radio-group v-model="editCompanyForm.isHadBrand">
                <el-radio :label="true">已有</el-radio>
                <el-radio :label="false">新建</el-radio>
              </el-radio-group>
              <div class="select-input-box">
                <el-select
                  v-model="editCompanyForm.brand"
                  placeholder="请选择所属品牌"
                  v-show="editCompanyForm.isHadBrand"
                >
                  <el-option
                    v-for="(item, index) in brandOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
                <el-input
                  v-show="!editCompanyForm.isHadBrand"
                  v-model="editCompanyForm.brand"
                  placeholder="请输入品牌名称"
                ></el-input>
              </div>
            </el-form-item>

            <el-form-item label="公司名称">
              <el-input
                v-model="editCompanyForm.companyName"
                placeholder="请输入公司名称"
              ></el-input>
            </el-form-item>

            <el-form-item label="到期时间">
              <el-date-picker
                v-model="editCompanyForm.dueDate"
                type="datetime"
                placeholder="选择到期时间"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="启用状态">
              <el-switch v-model="editCompanyForm.state" active-text="启用">
              </el-switch>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </bi-modal>
  </div>
</template>

<script>
import Modal from "../../components/Modal";
export default {
  components: {
    "bi-modal": Modal,
  },
  data() {
    return {
      screenForm: {
        brand: "", // 所属品牌
        companyName: "", // 公司名称
        state: "", // 当前状态
        registered: "", // 注册时间
      },
      brandOptions: [
        {
          value: "广汽三菱",
          label: "广汽三菱",
        },
        {
          value: "广汽本田",
          label: "广汽本田",
        },
        {
          value: "广汽丰田",
          label: "广汽丰田",
        },
        {
          value: "一汽大众",
          label: "一汽大众",
        },
      ],
      stateOptions: [
        {
          value: "全部",
          label: "全部",
        },
        {
          value: "正常",
          label: "正常",
        },
        {
          value: "到期",
          label: "到期",
        },
        {
          value: "禁用",
          label: "禁用",
        },
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      tableData: [
        {
          date: "2016-05-01 12:02:20",
          companyName: "河北佳信汽车销售有限公司",
          brand: "广汽三菱",
          state: "正常",
          registered: "2020-11-10 16:10:43",
          dueDate: "2020-11-17 16:10:43",
          dueDays: "7天",
        },
        {
          date: "2016-05-01 12:02:20",
          companyName: "河北佳信汽车销售有限公司",
          brand: "广汽三菱",
          state: "正常",
          registered: "2020-11-10 16:10:43",
          dueDate: "2020-11-17 16:10:43",
          dueDays: "7天",
        },
        {
          date: "2016-05-01 12:02:20",
          companyName: "河北佳信汽车销售有限公司",
          brand: "广汽三菱",
          state: "正常",
          registered: "2020-11-10 16:10:43",
          dueDate: "2020-11-17 16:10:43",
          dueDays: "7天",
        },
        {
          date: "2016-05-01 12:02:20",
          companyName: "河北佳信汽车销售有限公司",
          brand: "广汽三菱",
          state: "正常",
          registered: "2020-11-10 16:10:43",
          dueDate: "2020-11-17 16:10:43",
          dueDays: "7天",
        },
      ],
      currentPage: 1,
      showAddCompanyModal: false, // 控制 新增公司的modal显示
      addCompanyForm: {
        isHadBrand: true,
        brand: "",
        companyName: "",
        dueDate: "",
        state: "",
      }, // 新增公司 提交数据
      showEditCompanyModal: false, // 控制 编辑公司的modal显示
      editCompanyForm: {
        isHadBrand: true,
        brand: "",
        companyName: "",
        dueDate: "",
        state: "",
      }, // 编辑公司 提交数据
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  methods: {
    handleClick(row) {
      console.log("table row data", row);
      let {
        brand,
        companyName,
        dueDate,
        state,
      } = row;
      this.showEditCompanyModal = true;
      this.editCompanyForm = {
        title: companyName,
        isHadBrand: true,
        brand,
        companyName,
        dueDate,
        state: state == '正常' || '到期',
      };
    },
    // 分页插件方法
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    // 打开新增公司 modal
    openAddCompanyModal() {
      this.showAddCompanyModal = true;
    },
    // 新增公司 提交
    submitAddCompany() {},
    // 编辑公司 提交
    submitEditCompany() {},
  },
};
</script>
<style lang="less">
.userManagement {
  .header {
    .screen-box {
      .el-form {
        .alignCenter();
        flex-wrap: wrap;
        .el-form-item {
          .alignCenter();
          margin: 15px 0 0 10px;
          &:first-child {
            margin-left: 0;
          }
        }
        .search-btn {
          margin-left: 10px;
          margin-top: 15px;
        }
      }
    }
  }
  .data-box {
    margin-top: 20px;
    .titleTextBox {
      .titleTextBox();
    }
    .data-item-list {
      .alignCenter();
      .data-item {
        flex: 1;
        box-sizing: border-box;
        border-radius: 4px;
        margin-left: 10px;
        margin-top: 12px;
        &:first-child {
          margin-left: 0;
        }
        .title {
          color: @color-text-regular;
          font-weight: 500;
          text-align: center;
          margin-top: 5px;
        }
        .num {
          font-weight: 600;
          font-size: 48px;
          text-align: center;
        }
      }
    }
    .data-table-list {
      margin-top: 15px;
    }
  }
  .add-company-box {
    .el-form {
      .el-form-item {
        .el-form-item__label {
          text-align: left;
        }
        .alignCenter(flex, unset, unset, column);
        margin-bottom: 10px;
      }
    }
  }
}
</style>
